package com.example.piece.ui.activity

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.View
import androidx.databinding.ViewDataBinding
import com.therouter.router.Autowired
import com.therouter.router.Route
import com.example.base.ext.getViewModel
import com.example.base.tools.JsonTool
import com.example.base.vm.onError
import com.example.piece.BR
import com.example.piece.R
import com.example.piece.constant.Key
import com.example.piece.constant.RoutePath
import com.example.piece.databinding.ActivityMsgListBinding
import com.example.piece.databinding.ActivitySystemMsgDetailBindingImpl
import com.example.piece.entity.UserMessageSystemEntity
import com.example.piece.ui.adapter.MsgAdapter
import com.example.piece.ui.adapter.SystemMsgAdapter
import com.example.piece.vm.UserViewModel

/**
 * 消息列表
 */
@Route(path = RoutePath.MAIN_SYSTEM_MSG_LIST_ACTIVITY)
class MsgListActivity : Layer3Activity<ActivityMsgListBinding, ViewDataBinding>() {

    override val isTransparentStatusBar: Boolean = true
    override val isLockPortrait: Boolean = true
    override val statusBarAlpha: Int = 0
    override val isDarkMode: Boolean = true
    override val isFitsSystemWindows: Boolean = true
    override val leftIcon1Res: Int = R.drawable.ic_back
    override val centerLayoutRes: Int
        get() = R.layout.activity_msg_list
    override val title: String
        get() = H5_TITLE

    @JvmField
    @Autowired(name = Key.DataKey.H5_TITLE)
    var H5_TITLE: String = ""

    @JvmField
    @Autowired(name = Key.DataKey.MSG_TYPE)
    var MSG_TYPE: String = ""
    private var page: Int = 1
    private val adapter: MsgAdapter by lazy {
        val adapter = MsgAdapter()
        mCenterViewBinding?.rv?.adapter = adapter
        adapter
    }
    private val userViewModel: UserViewModel by lazy {
        getViewModel<UserViewModel>()
    }

    override fun onClickLeftIcon1(view: View) {
        onBackPressed()

    }

    override fun initData(savedInstanceState: Bundle?) {
        super.initData(savedInstanceState)
        val loadMoreModule = adapter.loadMoreModule
        loadMoreModule.setOnLoadMoreListener {
            getMsgList()
        }
        adapter.setEmptyView(R.layout.layout_empty)
        mCenterViewBinding?.srl?.setOnRefreshListener {
            page = 1
            loadMoreModule.isEnableLoadMore = true
            loadMoreModule.isAutoLoadMore = true
            getMsgList()

        }
        getMsgList()
    }

    private fun getMsgList() {
        userViewModel.userMessageList(this, page.toString(), MSG_TYPE) {
            mCenterViewBinding?.srl?.isRefreshing = false

            if (it.code == 200) {
                page++
                if (it.data.isNullOrEmpty()) {
                    adapter.loadMoreModule.isEnableLoadMore = false
                    adapter.loadMoreModule.isAutoLoadMore = false
                    return@userMessageList
                }
                if (page == 2) {
                    adapter.setList(it.data)

                } else {
                    adapter.addData(it.data)

                }

            }
        } onError {
            mCenterViewBinding?.srl?.isRefreshing = false

        }
    }

}